package com.java.mis;

import java.beans.PropertyEditorSupport;

public class WenumTypeEditor extends PropertyEditorSupport
{
  private final Class enumTypeClass;

  public WenumTypeEditor(Class wenumTypeClass)
    throws IllegalArgumentException
  {
    if ((wenumTypeClass == null) || (!(Enum.class.isAssignableFrom(wenumTypeClass)))) {
      throw new IllegalArgumentException("Property class must be a subclass of Enum");
    }
    this.enumTypeClass = wenumTypeClass;
  }

  public void setAsText(String text) throws IllegalArgumentException
  {
    Object enumTypeObj = null;

    if ((text != null) && (text != "")) {
      try {
        enumTypeObj = Enum.valueOf(this.enumTypeClass, text);
      }
      catch (IllegalArgumentException e) {
        enumTypeObj = null;
      }
    }

    setValue(enumTypeObj);
  }

  public void setValue(Object value)
  {
    super.setValue(value);
  }

  public String getAsText() {
    Object value = getValue();

    if (value == null) {
      return "";
    }

    return value.toString();
  }
}